
var hastouch = 'ontouchstart' in window ? true : false,                            //判断是否为移动端

    tapstart = hastouch ? 'touchstart' : 'mousedown',

    tapmove = hastouch ? 'touchmove' : 'mousemove',

    tapend = hastouch ? 'touchend' : 'mouseup';

var timeStart, timeEnd, time;//申明全局变量

function getTimeNow()//获取此刻时间
{
    var now = new Date();
    return now.getTime();

}

function holdDown_up()//鼠标按下时触发
{
    timeStart = getTimeNow();//获取鼠标按下时的时间
    time = setInterval(function ()//setInterval会每100毫秒执行一次
    {
        timeEnd = getTimeNow();//也就是每100毫秒获取一次时间
        if (timeEnd - timeStart > 100)//如果此时检测到的时间与第一次获取的时间差有1000毫秒
        {

            $.ajax({
                url: '/car_up',
                type: 'GET',
                success:function (data) {
                    var obj = JSON.parse(data);
                    var span = document.getElementById('cese');
                    span.innerText = span.innerText + obj['dong'];
                    console.log("长按上");
                }
            });
        }
    }, 10);
}


function holdDown_left()//鼠标按下时触发
{
    timeStart = getTimeNow();//获取鼠标按下时的时间
    time = setInterval(function ()//setInterval会每100毫秒执行一次
    {
        timeEnd = getTimeNow();//也就是每100毫秒获取一次时间
        if (timeEnd - timeStart > 100)//如果此时检测到的时间与第一次获取的时间差有1000毫秒
        {
             $.ajax({
                url: '/car_left',
                type: 'GET',
                success:function (data) {
                    var obj = JSON.parse(data);
                    var span = document.getElementById('cese');
                    span.innerText = span.innerText + obj['dong'];
                    console.log("长按左");//并弹出代码
                }
            });

        }
    }, 10);
}


function holdDown_right()//鼠标按下时触发
{
    timeStart = getTimeNow();//获取鼠标按下时的时间
    time = setInterval(function ()//setInterval会每100毫秒执行一次
    {
        timeEnd = getTimeNow();//也就是每100毫秒获取一次时间
        if (timeEnd - timeStart > 100)//如果此时检测到的时间与第一次获取的时间差有1000毫秒
        {
            $.ajax({
                url: '/car_right',
                type: 'GET',
                success:function (data) {
                    var obj = JSON.parse(data);
                    var span = document.getElementById('cese');
                    span.innerText = span.innerText + obj['dong'];
            // clearInterval(time)
                    console.log("长按右");//并弹出代码
                }
            });

        }
    }, 10);
}


function holdDown_down()//鼠标按下时触发
{
    timeStart = getTimeNow();//获取鼠标按下时的时间
    time = setInterval(function ()//setInterval会每100毫秒执行一次
    {
        timeEnd = getTimeNow();//也就是每100毫秒获取一次时间
        if (timeEnd - timeStart > 100)//如果此时检测到的时间与第一次获取的时间差有1000毫秒
        {
            $.ajax({
                url: '/car_down',
                type: 'GET',
                success:function (data) {
                    var obj = JSON.parse(data);
                    var span = document.getElementById('cese');
                    span.innerText = span.innerText + obj['dong'];
                    console.log("长按下");//并弹出代码
                }
            });

        }
    }, 10);
}


function all_1() {
    clearInterval(time);
    var span = document.getElementById('cese');

    span.innerText = span.innerText + '释放';

    console.log('释放')
}

document.getElementById('up').addEventListener(tapstart, holdDown_up);
document.getElementById('up').addEventListener(tapend, all_1);

document.getElementById('left').addEventListener(tapstart, holdDown_left);
document.getElementById('left').addEventListener(tapend, all_1);

document.getElementById('right').addEventListener(tapstart, holdDown_right);
document.getElementById('right').addEventListener(tapend, all_1);

document.getElementById('down').addEventListener(tapstart, holdDown_down);
document.getElementById('down').addEventListener(tapend, all_1);

